/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import java.io.File;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.DateTools;
import javacommon.util.Notes;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.model.Attach;
import com.tech15.core.service.AttachManager;
import com.tech15.pub.dao.PubDataDao;
import com.tech15.pub.dao.PubOfficeAuthDao;
import com.tech15.pub.dao.PubOfficeDao;
import com.tech15.pub.model.PubData;
import com.tech15.pub.model.PubOffice;
import com.tech15.pub.model.PubOfficeAuth;
import com.tech15.pub.vo.query.PubOfficeQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PubOfficeManager extends BaseManager<PubOffice,java.lang.String>{

	private PubOfficeDao pubOfficeDao;
	private AttachManager attachManager;
	private PubDataDao pubDataDao;
	private PubOfficeAuthDao pubOfficeAuthDao;
	
	
	public void setPubOfficeAuthDao(PubOfficeAuthDao pubOfficeAuthDao) {
		this.pubOfficeAuthDao = pubOfficeAuthDao;
	}
	public void setPubDataDao(PubDataDao pubDataDao) {
		this.pubDataDao = pubDataDao;
	}
	public void setAttachManager(AttachManager attachManager) {
		this.attachManager = attachManager;
	}
	public void setPubOfficeDao(PubOfficeDao dao) {
		this.pubOfficeDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.pubOfficeDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PubOfficeQuery query) {
		return pubOfficeDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public PubOffice getById(String id){
		PubOffice pubOffice = pubOfficeDao.getById(id);
		
		if(ObjectUtils.isNotEmpty(pubOffice.getLb())){
			PubData pd = pubDataDao.getById(new Long(pubOffice.getLb()));
			if(pd != null){
				pubOffice.setCategoryName(pd.getName());
			}
		}
		
		if(ObjectUtils.isNotEmpty(pubOffice.getLb2())){
			PubData pd = pubDataDao.getById(new Long(pubOffice.getLb2()));
			if(pd != null){
				pubOffice.setDataname(pd.getName());
			}
		}
		
		List<Attach> attach = attachManager.findByTableNameAndId(Notes.TALBE_PUB_OFFICE, ""+id);
		pubOffice.setAttach(attach);
		
		List<PubOfficeAuth> poalist = pubOfficeAuthDao.searchById(id);
		pubOffice.setUsers(poalist);
		return pubOffice;
	}
	
	public void saveOffice(PubOffice pubOffice,String realpath,List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType){
		if(pubOffice.getUserids() != null && pubOffice.getUserids().length>0){
			pubOffice.setIsauth(1);
		}else{
			pubOffice.setIsauth(0);
		}
		pubOffice.setUptime(DateTools.getCurrentTime());
		pubOfficeDao.save(pubOffice);
		pubOfficeDao.flush();
		if(attachFile != null  && attachFileFileName != null){
			attachManager.saveAttachs(realpath, pubOffice.getUserid(), Notes.TALBE_PUB_OFFICE, ""+pubOffice.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
		
		if(pubOffice.getUserids() != null){
			for(Long userid : pubOffice.getUserids()){
				PubOfficeAuth poa = new PubOfficeAuth();
				poa.setUserid(userid);
				poa.setPoid(pubOffice.getId());
				pubOfficeAuthDao.save(poa);
			}
		}
	}
	
	public void updateOffice(PubOffice pubOffice,String realpath,List<File> attachFile, List<String> attachFileFileName, List<String> attachFileContentType){
		if(pubOffice.getUserids() != null && pubOffice.getUserids().length>0){
			pubOffice.setIsauth(1);
		}else{
			pubOffice.setIsauth(0);
		}
		pubOffice.setUptime(DateTools.getCurrentTime());
		pubOfficeDao.update(pubOffice);
		/*if(attachFile != null){
			attachManager.delete(Notes.TABLE_PUB_TEMPLATE, pubOffice.getId());
			attachManager.saveAttachs(realpath, pubOffice.getUserid(), Notes.TABLE_PUB_TEMPLATE, ""+pubOffice.getId(), attachFile, attachFileFileName, attachFileContentType);
		}*/
		
		if(!ObjectUtils.isNullOrEmptyString(pubOffice.getUploadedAttachName())){
			String attachids = pubOffice.getUploadedAttachName().replaceAll(",", "','");
			attachids = attachids.replaceAll(" ", "");
			attachids = "'"+attachids+"'";
			attachManager.delete("delete attach t where t.tablename='"+Notes.TALBE_PUB_OFFICE+"' and t.tableid='"+pubOffice.getId()+"' and t.attachid not in ("+attachids+")");
		}
		if(attachFile != null  && attachFileFileName != null){
			attachManager.saveAttachs(realpath, pubOffice.getUserid(), Notes.TALBE_PUB_OFFICE, ""+pubOffice.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
		
		if(pubOffice.getUserids() != null){
			pubOfficeAuthDao.delete("delete pub_office_auth a where a.poid='"+pubOffice.getId()+"'");
			for(Long userid : pubOffice.getUserids()){
				PubOfficeAuth poa = new PubOfficeAuth();
				poa.setUserid(userid);
				poa.setPoid(pubOffice.getId());
				pubOfficeAuthDao.save(poa);
			}
		}
	}
	public Page searchIndex(PubOfficeQuery query) {
		return pubOfficeDao.searchIndex(query);
	}
}
